package edu.fzu.servicebase.exceptionhandler;

import edu.fzu.commonutils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 指定出现什么异常时执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult error(Exception e){
        e.printStackTrace();
        return ResponseResult.error().message("执行了全局异常处理");
    }

    // 处理特定的异常
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public ResponseResult error(ArithmeticException e){
        e.printStackTrace();
        return ResponseResult.error().message("执行了数字运算异常处理");
    }

    // 自定义异常
    @ExceptionHandler(GuliException.class)
    @ResponseBody
    public ResponseResult error(GuliException e){
        log.error(e.getMessage());
        return ResponseResult.error().code(e.getCode()).message(e.getMsg());
    }
}
